﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Huirui.Cavan.Core.Extension;
using AutoMapper;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class SupplierController : EntityController<Supplier, string, SupplierSearch, ISupplierViewModel, ISupplierAppService>, ISupplierController
    {
        private IAreaAppService AreaAppService { get; set; }
        private ISupplierTypeAppService SupplierTypeAppService { get; set; }

        public override string ControllerName
        {
            get { return ControllerConst.SUPPLIER; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public SupplierController()
        {
            this.Model = ModelFactory.CreateModel<ISupplierViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "supplier_query")]
        public ActionResult AddSupplier(string id)
        {
            if (id.IsNotEmpty())
            {
                this.Model.Item = this.AppService.GetById(id);
            }
            InitSupplierTypes(this.Model, true);
            return View("~/Views/System/v1/addSupplier.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "supplier_edit")]
        public JsonResult AddSupplier(SupplierViewModel model)
        {
            BoolResult jsonResult = null;

            model.Item.Area = this.AreaAppService.GetById(model.Item.Area.Id);
            model.Item.Type = this.SupplierTypeAppService.GetById(model.Item.Type.Id);
            model.Item.Nature = (SystemIdentifier.SupplierNature)model.Nature;
            model.Item.PayType = (SystemIdentifier.PayType)model.PayType;
            if (model.NoPrefixId.IsNotEmpty())
            {
                model.Item.Id = SecurityAppService.Corporation.Id + "_" + model.NoPrefixId;
            }
            BoolGenericResult<Supplier> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "supplier_query")]
        public JsonResult GetSuppliers(SupplierViewModel model)
        {
            SupplierJsonViewModel json = new SupplierJsonViewModel();
            json.rows = new List<SupplierJson>();

            long total = 0;
            IList<Supplier> suppliers = this.AppService.GetBySearch(SearchFactory.CreateSupplierSearch(model.SearchParam.ID, model.SearchParam.Name, model.SearchParam.Contact, model.SearchParam.SupplierTypeID, model.SearchParam.AreaCode), out total).ToList();
            foreach (Supplier s in suppliers)
            {
                SupplierJson sj = Mapper.Map<Supplier, SupplierJson>(s);
                json.rows.Add(sj);
            }
            json.total = total;

            return Json(json, "application/json");
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "supplier_query")]
        public ActionResult GetSuppliers()
        {
            this.Model.Purpose = RouteData.Values["purpose"].ToString();
            return PartialView("~/Views/System/v1/partial/selectSupplierPartial.cshtml", this.Model);
        }

        [ActionName(ActionConst.LIST)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "supplier_query")]
        public ActionResult GetSuppliersList()
        {
            InitSupplierTypes(this.Model, false);
            return PartialView("~/Views/System/v1/supplierList.cshtml", this.Model);
        }

        [ActionName(ActionConst.DETAIL)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "supplier_query")]
        public ActionResult GetSupplierDetail(string supplierid)
        {
            Supplier supplier = this.AppService.GetById(supplierid);
            this.Model.Item = supplier;
            this.Model.ProvinceID = supplier.Area.ParentCode;
            this.Model.CityID = supplier.Area.Id;
            this.Model.EmployeeRole = supplier.EmployeeRole.Id;
            this.Model.Nature = supplier.Nature.GetHashCode();
            this.Model.PayType = supplier.PayType.GetHashCode();
            this.Model.NoPrefixId = supplier.Id.Substring((SecurityAppService.Corporation.Id+"_").Length);
            InitSupplierTypes(this.Model,true);
            return PartialView("~/Views/System/v1/addSupplier.cshtml", this.Model);
        }

        private void InitSupplierTypes(ISupplierViewModel model, bool IsShowAdd)
        {
            IList<SupplierType> supplierTypes = this.SupplierTypeAppService.GetBySearch(SearchFactory.CreateSupplierTypeSearch()).ToList();
            foreach (SupplierType st in supplierTypes)
            {
                SelectListItem item = new SelectListItem() { Text = st.Name, Value = st.Id.ToString() };
                if (this.Model.Item.IsNotNull() && this.Model.Item.Type.Id == st.Id)
                {
                    item.Selected = true;
                }
                model.SupplierTypes.Add(item);
            }
            if (IsShowAdd)
            {
                SelectListItem addItem = new SelectListItem() { Text = "[新增...]", Value = "0" };
                model.SupplierTypes.Add(addItem);
            }
        }
    }
}
